package com.phpxiu.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.Banner;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.UIBanner;
import com.phpxiu.app.view.custom.FrescoImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/5/6.
 */
public class HomeHotBannerAdapter extends PagerAdapter {
    private final static String TAG="IndexPhotoWallPagerAdapter";
    private Context mContext;
    private List<Banner> banners=new ArrayList<Banner>();
    private LayoutInflater mInflater;
    private boolean mIsPageChange=false;
    public HomeHotBannerAdapter(Context context, List<Banner> banners) {
        mContext = context;
        this.banners = banners;
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return banners.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        FrescoImageView imageView=(FrescoImageView)mInflater.inflate(R.layout.home_hot_banner_view,container,false);
        String url=banners.get(position).getImg();
        if(url!=null&&!"".equals(url)) {
            imageView.setImageURI(Uri.parse(url));
            imageView.setOnClickListener(new OnItemClickListener(position));
        }
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    class OnItemClickListener implements View.OnClickListener{
        private int position=0;

        OnItemClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            KKYUtil.log(TAG, "点击了广告连接：" + banners.get(position).getTitle() + "@" + banners.get(position).getLink());
            Intent bannerWeb=new Intent(mContext, UIBanner.class);
            bannerWeb.putExtra(UIBanner.BANNER_WEB_TITLE,banners.get(position).getTitle());
            bannerWeb.putExtra(UIBanner.BANNER_WEB_URL,banners.get(position).getLink());
            mContext.startActivity(bannerWeb);
        }
    }

    public void refresh(boolean isPageChange){
        mIsPageChange=isPageChange;
        notifyDataSetChanged();
        mIsPageChange=false;
    }

    @Override
    public int getItemPosition(Object object) {
        if(mIsPageChange){
            return POSITION_NONE;
        }else {
            return super.getItemPosition(object);
        }
    }
}
